package com.stylefeng.guns.modular.system.api;

import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.response.JsonResult;
import com.stylefeng.guns.core.response.ResultGenerator;
import com.stylefeng.guns.modular.system.model.Ad;
import com.stylefeng.guns.modular.system.service.IAdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping
public class AdApiController extends BaseController {

    @Autowired
    IAdService iAdService;

    @RequestMapping(value = "/getAd/{channel}")
    public JsonResult getAdByChannel(@PathVariable String channel){

        Map<String,Object> map = new HashMap<String, Object>();
        try {
            List<Ad> adList = iAdService.getAd(channel);
            if(adList == null || adList.size() == 0){
                return  ResultGenerator.genFailResult("return null");
            }else{
                map.put("adList",adList);
                map.put("popNum",adList.get(0).getPopNum());
                map.put("popTime",adList.get(0).getPopTime());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("error");
        } finally {

        }
        return ResultGenerator.genSuccessResult(map);
    }

}
